package com.mj.mafeed.service;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;

import com.mj.mafeed.bean.AreaJSON;
import com.mj.mafeed.dao.AreaDao;

import android.content.Context;

/**
 * 地区
 * @author zhaominglei
 * @date 2014-12-11
 * 
 */
public class AreaService extends BaseService {
	private AreaDao areaDao = new AreaDao();
	/**
	 * 同步地区
	 */
	public void syncArea(Context context) {
		int count = areaDao.getCount(context);
		if (count > 0) {
			return;
		}
		InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("config/feed_area.sql");
		BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
		String sql = null;
		try {
			while((sql = br.readLine()) != null) {
				areaDao.save(context, sql);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 获取省份
	 */
	public List<AreaJSON> getProvince(Context context) {
		return areaDao.getChildren(context, null);
	}
	
	/**
	 * 获取市
	 */
	public List<AreaJSON> getCity(Context context, Long parentId) {
		return areaDao.getChildren(context, parentId);
	}
	
	/**
	 * 获取县
	 */
	public List<AreaJSON> getCounty(Context context, Long parentId) {
		return areaDao.getChildren(context, parentId);
	}
	
	/**
	 * 获取地区
	 */
	public AreaJSON getById(Context context, Long areaId) {
		return areaDao.getById(context, areaId);
	}
}
